package com.ruiyi.subway.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class FileUtil {
	
	public final static String UPLOAD_DIRECTORY="upload";
	/**
	 * 取得文件扩展名
	 * @param fileName 文件全名
	 * @return
	 */
	public static String getExtension(String fileName){
		int par=fileName.lastIndexOf(".");
		String extension="";
		if (par!=-1){
			extension=fileName.substring(par);
		}	
		return extension;
	}
	
	/**
	 * 取得文件名
	 * @param fileName 文件全名
	 * @return
	 */
	public static String getFileName(String fileName){
		int par=fileName.lastIndexOf(".");
		String name;
		if (par==-1){
			name=fileName;
		}else{
			name=fileName.substring(0,par);
		}
		return name;
	}
	
	//自定义文件上传的名称
	public static String defineUploadFileName(){
		return System.currentTimeMillis()+"";
	}
	
	
	public static String getAppRealPath(HttpServletRequest request){
		String appPath=request.getSession().getServletContext()
				.getRealPath("/");
		return appPath;
	}
	
	/**
	 * 创建路径规则
	 * 
	 * @return
	 */
	public static Map<String,String> getFilePath(HttpServletRequest request) {
		String basePath=getAppRealPath(request);
		String currentTime = DateUtils.getDateForChar();
		String filePath = basePath+File.separator+UPLOAD_DIRECTORY+
				File.separator+currentTime+File.separator;
		String requestPath="/"+UPLOAD_DIRECTORY+"/"+currentTime+"/";
		File file = new File(filePath);
		if(!file.exists()||!file.isDirectory()){
			file.mkdirs();
		}
		Map<String,String> pathMap=new HashMap<String,String>();
		pathMap.put("filePath", filePath);
		pathMap.put("requestPath", requestPath);
		return pathMap;
	}
	
	
	public static void main(String[] args) {
		
	}

}
